package example.bill.com.pthreadndk;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnStart;
    private Button btnStop;
    private Button btnKillSig;

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());

        btnStart = (Button)findViewById(R.id.btn_start);
        btnStop = (Button)findViewById(R.id.btn_stop);
        btnKillSig = (Button)findViewById(R.id.btn_killsig);

        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        btnKillSig.setOnClickListener(this);
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    /**
     * start a new thread
    * */
    public native int startPthread();

    /**
     * start a new thread
     * */
    public native int stopPthread();

    /**
     * kill signal to thread
     * */
    public native int killSig();

    @Override
    public void onClick(View view) {
        int resId = view.getId();
        switch (resId){
            case R.id.btn_start:
                startPthread();
                break;
            case R.id.btn_stop:
                stopPthread();
                break;
            case R.id.btn_killsig:
                killSig();
                break;
        }
    }
}
